<template>
  <div>
    <input name="file" type="file" :id="id" @change="container" style="display:none" :multiple="multiple" />
    <label :for="id" class="txt" ref="loadChange">{{message}}</label>
  </div>
</template>
<style scoped>
  .txt {
    font-size: 12px;
    color: #77A4FF;
    cursor: pointer;
  }

</style>
<script>
  import axios from 'axios'
  export default {
    name: 'upLoadFile',
    data() {
      return {
        fileName: "",
        uploadParam: "",
        id: Number(Math.random().toString().substr(3, 16) + Date.now()).toString(36)
      }
    },
    methods: {
      container(e) {
        for (let i of e.target.files) {
          this.update(i);
        }
      },
      update(file) { // 上传照片路径
        console.log(file)
        let index = file.name.lastIndexOf(".");
        let ext = file.name.substr(index + 1);
        let bool = false;
        let message = "";
        if (this.rules) {
          for (let i of this.rules) {
            if (i.rule instanceof Array) {
              for (let j of i.rule) {
                if (ext == j) {
                  bool = true;
                }
              }
            } else {

              if (ext == i.rule) {
                bool = true;
              }
            }
            message = i.message;
          }
        } else {
          bool = true;
        }
        if (bool) {
          this.$emit("funLocal", {
            success: file
          });
          this.submitUpload(file);


        } else {
          this.$emit("fun", {
            error: message
          });
          this.$emit("funLocal", {
            error: message
          });
        }



      },
      submitUpload(file) {
        console.log(file);
        var uploadParam = new FormData();
        //let url="http://39.96.58.238:9100/oss/uploadFileForDisk"
        let url = "/bmOssFile/uploadFileForDisk";
        uploadParam.file = null;
        uploadParam.append('file', file, file.name);
        let self = this;
        axios({
            url: url,
            data: uploadParam,
            method: 'post',
            onUploadProgress(progressEvent) {
              let complete = (progressEvent.loaded / progressEvent.total * 100 | 0) + '%';
              let obj = {
                size: (file.size / 1024).toFixed(2),
                name: file.name,
                progress: complete
              };
              self.$emit("progress", obj);

            }
          })
          .then(res => {
            if (res.data.code == 200) {
              let data = Object.assign(file, res.data.data)
              this.$emit("fun", {
                success: data
              });
            } else {
              this.$emit("fun", {
                error: res.data.message
              });
            }

            var test = document.getElementById(this.id);
            test.value = '';
          }).catch((err) => {
            var test = document.getElementById(this.id);
            test.value = '';
            this.$message.error("上传失败");
          })



      }
    },
    props: ['message', 'rules', 'fun', 'progress', 'funLocal', 'multiple']
  }

</script>
